import tensorflow as tf
import numpy as np

SEED = 23455

profit = 99
cost = 1

rdm = np.random.RandomState(seed=SEED)
x = rdm.rand(32,2)#两个影响因素，现在先随机生成

y_ = [[x1+x2+(rdm.rand()/10-0.05)] for (x1,x2) in x]
x = tf.cast(x,dtype=tf.float32)

w1 = tf.Variable(tf.random.normal([2,1],stddev=1,seed=1))
epoch = 15000
lr = 0.002

for epoch in range(epoch):
    with tf.GradientTape() as tape:
        y = tf.matmul(x,w1)
        #loss_mse = tf.reduce_mean(tf.square(y_-y))#损失函数
        loss_mse = tf.reduce_mean(tf.where(tf.greater(y,y_),(y-y_)*cost,(y_-y)*profit))#自定义损失函数
    grads = tape.gradient(loss_mse,w1)
    w1.assign_sub(lr*grads)
    if epoch % 500 == 0:
        print("After %d training steps,w1 is"%(epoch))
        print(w1.numpy(),"\n")
print("Final w1 is:",w1.numpy())
